package com.alander.nexusgo.user.controller;

import com.alander.nexusgo.common.response.Result;
import com.alander.nexusgo.framework.web.starter.controller.BaseController;
import com.alander.nexusgo.user.model.entity.UserPreferences;
import com.alander.nexusgo.user.service.UserPreferencesService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户偏好设置控制器
 */
@RestController
@RequestMapping("/api/user/preferences")
@RequiredArgsConstructor
public class UserPreferencesController extends BaseController {

    private final UserPreferencesService userPreferencesService;

    /**
     * 获取用户偏好设置
     *
     * @return 用户偏好设置
     */
    @GetMapping
    public Result<UserPreferences> getUserPreferences() {
        Long userId = getUserId();
        UserPreferences preferences = userPreferencesService.getUserPreferences(userId);
        return success(preferences);
    }

    /**
     * 更新用户偏好设置
     *
     * @param preferences 用户偏好设置
     * @return 更新结果
     */
    @PutMapping
    public Result<Boolean> updateUserPreferences(@RequestBody UserPreferences preferences) {
        Long userId = getUserId();
        boolean result = userPreferencesService.updateUserPreferences(userId, preferences);
        return result ? success(true) : fail("更新失败");
    }

    /**
     * 重置用户偏好设置为默认值
     *
     * @return 重置结果
     */
    @PostMapping("/reset")
    public Result<UserPreferences> resetUserPreferences() {
        Long userId = getUserId();
        UserPreferences preferences = userPreferencesService.initUserPreferences(userId);
        return success(preferences);
    }
}
